from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)


# 创建引擎和表
engine = create_engine('sqlite:///example.db')
Base.metadata.create_all(engine)

# 插入数据
Session = sessionmaker(bind=engine)
session = Session()
session.add(User(name="Charlie", age=30))
session.commit()

# 查询数据
users = session.query(User).all()
for user in users:
    print(user.name, user.age)
